<?php

require_once(APPLICATION_PATH.'/models/User.php');
require_once(APPLICATION_PATH.'/models/Items.php');
require_once(APPLICATION_PATH.'/models/VotedIps.php');
require_once(APPLICATION_PATH.'/models/FilterIps.php');
require_once(APPLICATION_PATH.'/models/Record.php');
require_once(APPLICATION_PATH.'/dbres/RecordRes.php');
require_once('BaseController.php');

class IndexController extends BaseController
{
    //首页
    public function indexAction()
    {
        $this->view->title = "首页";
        $this->view->home = 'home';
        $this->render('home');
    }
    
    public function aboutAction()
    {
        $this->view->title = "关于我们";
        $this->view->about = 'about';
        $this->render('about');
    }
    
    //遍历选项
    public function recordlistAction()
    {
        $this->view->title = "账号列表";
        $this->view->recordlist = 'recordlist';
        $numPerPage = 10;
        $getPage = $this->getRequest()->getParam('page');
        $page = empty($getPage) ? 1 : $getPage;
        $search = $this->getRequest()->getParam('search');
        $search = isset($search) ?  $search : '';
        $recordRes = new RecordRes();
        $res = $recordRes->recordListRes($search, $page, $numPerPage);
        //print_r($res);exit;
        
        //把分页显示到view
        $this->view->data = $res['dbres'];
        $this->view->search = $search;
        $this->view->paginator = $res['paginator'];

        $this->render('record');
        //Zend_Debug::dump($this->view->items);
    }
    
    //添加记录
    public function addrecordAction()
    {   
        $request = $this->getRequest();
        
        if($request->isPost())
        {
            //$this->view->title = "添加账号";
            $countTag = $request->getParam('count_tag');
            $countName = $request->getParam('count_name');
            $countPasswd = $request->getParam('count_passwd');
            
            $res = $this->checkFormData($countTag, $countName, $countPasswd);
            $this->view->info = $res;
            if($res['error'] != 1)
            {
                $recordModel = new Record();
                $data = array(
                    'count_tag' => $countTag,
                    'count_name' => $countName,
                    'count_passwd' => $countPasswd
                    );
                if($recordModel->insert($data) >0)
                {
                    $this->view->info['res_message'] = '添加记录成功！';
                }
                else 
                {
                    $this->view->info['res_message'] = '添加记录失败！';
                }
            }
            else
            {
                $this->view->info['res_message'] = '操作失败！';
            }
            $this->render('response');
        }
        else
        {
            $this->view->title = "添加账号";
            $this->render('addrecord');
        }
    }
    
    //编辑 记录
    public function editrecordAction()
    {
        $request = $this->getRequest();
        $id = $this->getRequest()->getParam('id');
        $this->view->title = "编辑账号";
        if($request->isPost())
        {
            $countTag = $request->getParam('count_tag');
            $countName = $request->getParam('count_name');
            $countPasswd = $request->getParam('count_passwd');
            
            $res = $this->checkFormData($countTag, $countName, $countPasswd);
            $this->view->info = $res;
            if($res['error'] != 1)
            {
                $recordModel = new Record();
                $data = array(
                    'count_tag' => $countTag,
                    'count_name' => $countName,
                    'count_passwd' => $countPasswd
                    );
                
                $db = $recordModel->getAdapter();
                $where = $db->quoteInto('id = ?', $id);
                $recordModel->update($data, $where);
                
                if($recordModel->update($data, $where)==0)
                {
                    $this->view->info['res_message'] = '更新记录成功！';
                }
                else 
                {
                    $this->view->info['res_message'] = '更新记录失败！';
                }
            }
            else
            {
                $this->view->info['res_message'] = '操作失败！';
            }
            $this->render('response');
        }
        else
        {
            $recordModle = new Record(); 
            $res = $recordModle->fetchRow('id='.$id)->toArray();
            //Zend_Debug::dump($res);
            $this->view->record = $res;
            
            $this->render('editrecord');
        }
    }
    
    //删除记录
    public function delrecordAction()
    {   
        $this->_helper->viewRenderer->setNoRender(); 
        $this->_helper->layout()->disableLayout();
        
        $request = $this->getRequest();
        $id = $this->getRequest()->getParam('id');
        
        $recordModle = new Record();
        $where = 'id = ' . $id;
        $rows_affected = $recordModle->delete($where); 
        ($rows_affected == 1) ? $temp['success'] = 1 :$temp['success'] = 0;
        
        //$temp['success'] = 1; 
        $json=Zend_Json::encode($temp);
        echo $json;
    }
    
    
    //投票
    public function voteAction()
    {
        $item_id = $this->getRequest()->getParam('id');
        $ip = $this->getRequest()->getServer('REMOTE_ADDR');
        $today = date('Ymd');
        
        $votedIps = new VotedIps();
        $where = "ip='$ip' AND date_time=$today";
        $res = $votedIps->fetchAll($where)->toArray();
        
        if(count($res)>11)
        {
            $this->render('error');
            return;
        }
        else
        {
            $data = array(
                'ip' => $ip,
                'item_id' => $item_id,
                'date_time' => $today
            );
            
            if($votedIps->insert($data)>0)
            {
                $item = new Items();
                $items = $item->find($item_id)->toArray();
                $newcounts = $items[0]['counts']+1;
                $setData = array('counts' => $newcounts);
                //$where = "id=$item_id";
                $db = $item->getAdapter();
                $where = $db->quoteInto('id = ?', $item_id);
                $item->update($setData, $where);
            }
        }

        $this->render('index');
    }
    
    //后台验证表单数据
    private function checkFormData($count_tag, $count_name, $count_passwd)
    {   
        if(empty($count_tag) || empty($count_name) || empty($count_passwd))
        {
            $info['error'] = 1;
            $info['message'] = '不能为空！';
        }
        return $info;
    }
    
}

